package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.utils.RedisUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 业务聚合
 *
 * @version [V2.0]
 * @package com.example.controller;
 * @description:[用一句话描述该文件做什么]
 * @author: [zhuben]
 * @date: [2024-02-03 16:07]
 * @copyright: 2024 echovin.com
 */

@RestController
@RequestMapping("redis")
public class RedisController {
    @Resource
    private RedisUtils redisUtils;

    @PostMapping("keys")
    public ResponseEntity<?> keys(@RequestBody String requestInfo){
        String pattern = "*";
        if (requestInfo == null){
            return ResponseEntity.ok("");
        }
        JSONObject jsonObject = JSONObject.parseObject(requestInfo);
        pattern = jsonObject.getString("pattern");
        Set<String> scan = redisUtils.scan(pattern);
        return ResponseEntity.ok(scan);
    }

    @PostMapping("values")
    public ResponseEntity<?> values(@RequestBody String requestInfo) throws Exception {
        if (requestInfo == null){
            return ResponseEntity.ok("");
        }
        JSONObject jsonObject = JSONObject.parseObject(requestInfo);
        String key = jsonObject.getString("key");
        int page = jsonObject.getInteger("page");
        int size = jsonObject.getInteger("size");
        RedisUtils.Page ofPage = redisUtils.ofPage(key, page, size);
        return ResponseEntity.ok(ofPage);
    }
}
